package services

import (
	wms "ez/apps/wms/document"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
)

const IoTypeIn = 1
const IoTypeOut = 2
const IoTypeLeft = 3

type Summary struct {
	Y      int //年月日
	M      int //年月日
	D      int //年月日
	Start  int //区间开始（含）
	End    int //区间结束（不含）
	Rows   []*wms.Summary
	IoType int
}

func (this *Summary) GetRows() error {
	crud := wms.NewSummaryCrud()
	//crud.Factory.SetAssociations("Item", "Type")
	cond := ss.M{}
	if this.Y > 0 {
		cond["y"] = this.Y
	}
	if this.M > 0 {
		cond["m"] = this.M
	}
	if this.D > 0 {
		cond["d"] = this.D
	}
	if this.Start > 0 {
		cond["ymd__gte"] = this.Start
	}
	if this.End > 0 {
		cond["ymd__lt"] = this.End
	}
	if this.IoType > 0 {
		cond["typeId"] = this.IoType
	}
	rows, e := crud.FindBy(cond, []string{"ymd"}, 0, 0)
	this.Rows = rows
	return e
}

func (this *Summary) GetItemByMonth() []*wms.ItemSku {
	if this.Rows == nil {
		ez.Try(this.GetRows())
	}
	items := make([]*wms.ItemSku, 0)
	itemsMap := make(map[int64]*wms.ItemSku, 0)
	for _, row := range this.Rows {
		itemsMap[row.ItemId] = row.Item
	}
	for _, sku := range itemsMap {
		items = append(items, sku)
	}
	return items
}
